/*******************************************************************
 *
 * Copyright (C), 2021-2022, Letstrong, All rights reserved.
 *
 * Author      : 
 * Create Time : 2020-12-14
 * Version     : v0.1
 * Description : 
 *******************************************************************/
 
/*----------------------------Head file----------------------------*/
#include "icu.h"
#include "wdt.h"
#include "uart.h"
#include "hal_utc.h"
#include "hal_gpio.h"
#include "hal_motor.h"
#include "hal_flash.h"
#include "hal_battery.h"
#include "hal_pwr_mgr.h"
#include "app_misc.h"
#include "app_reset.h"
#include "app_battery.h"
#include "app_display.h"
#include "app_scheduler.h"
#include "app_store_mem.h"
#include "app_global_data.h"
#include "app_store_set_info.h"
#include "user_config.h"

/*----------------------------macro file---------------------------*/

/*----------------------------type define--------------------------*/

/*----------------------------var define---------------------------*/

/*-------------------------func declaration------------------------*/

/*-----------------------------------------------------------------*/

void app_peripheral_init(void)
{
	hal_gpio_init();
	hal_motor_stop();
	app_system_reset_data_init();
	app_mem_data_init();
	app_set_info_init();
	app_batt_hw_init();
	g_app_data.sys.state.btn_lock = true;
	hal_pwr_sleep_enable();	//enable low power mode in init process, or disable API is invalid. Fix hall count inaccurate when first power on.
}


void app_ship_mode_enter(void)
{
	uint16_t reset_reason = SHIP_MODE_WAKE_ID;
	hal_flash_write(TEMP_DATA_RST_ADDR, (uint8_t*)&reset_reason, sizeof(reset_reason));

	while(0 == hal_gpio_read(BUTTON1_PIN_NUM)) {	//Wait button release.
		wdt_feed(0x3fff);
		app_delay_ms(50);
	}
	hal_pwr_sleep_enable();
	uart_int_disable();
	hal_gpio_int_disable(HALL1_PIN_NUM);
	hal_gpio_int_disable(HALL2_PIN_NUM);
	app_disp_hw_deinit();
	hal_motor_stop();
	hal_batt_adc_deinit();
	hal_batt_charge_hw_deinit();
#if(BOOST_POWER_CTRL_EN)
	app_peripheral_boost_power_disable();
#endif
	GLOBAL_INT_STOP();
	g_app_data.sys.mode = DEV_MODE_DEEP_SLEEP;
	enter_deep_sleep();
}

#if(BOOST_POWER_CTRL_EN)
void app_peripheral_boost_power_enable(void)
{
	hal_gpio_output_cfg(BOOST_PWR_PIN_NUM, PULL_UP);
	hal_gpio_write(BOOST_PWR_PIN_NUM, 1);
	app_delay_ms(50);
}

void app_peripheral_boost_power_disable(void)
{
	hal_gpio_output_cfg(BOOST_PWR_PIN_NUM, PULL_DOWN);
	hal_gpio_write(BOOST_PWR_PIN_NUM, 0);
}
#endif
